function dish_add(dishID, mid) {
	let xmlhttp;
	let single_order;
	let cart_basket = document.getElementById("cart_basket");
	if (document.getElementById(dishID) == null){
		single_order = createSingleOrder(dishID, mid);
		cart_basket.insertBefore(single_order,cart_basket.firstChild);
	}else {
		single_order = document.getElementById(dishID);
	}
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState === 4 && xmlhttp.status === 200){
			let dish = JSON.parse(xmlhttp.responseText)[0];
			single_order.getElementsByClassName("order-food-name")[0].children[0].innerText = dish.dname;
			let value = single_order.getElementsByClassName("input-num")[0];
			let value2 = document.getElementById(`dish${dishID}`).getElementsByClassName("food-number")[0];
			if (value.getAttribute("value") !== ""){
				value.setAttribute("value", parseInt(value.getAttribute("value")) + 1 + "");
				value2.setAttribute("value", parseInt(value2.getAttribute("value")) + 1 + "");
			}else {
				value.setAttribute("value", '1');
				value2.setAttribute("value", '1');
				document.getElementById(`dish${dishID}`).getElementsByClassName("food-introduce-addcart")[0].setAttribute("style", "display: none");
				document.getElementById(`dish${dishID}`).getElementsByClassName("food-introduce-form")[0].setAttribute("style", "display: table");
			}
			single_order.getElementsByClassName("replace-money")[0].innerHTML = "￥" + (dish.price*dish.discount*value.getAttribute("value")).toFixed(2);

			let piece = document.getElementById("piece");
			if (piece != null){
				piece.innerText = parseInt(piece.innerText) + 1 + "";
			}else {
				piece = document.createElement("span");
				piece.setAttribute("class", "cart-pieces");
				piece.setAttribute("id", "piece");
				piece.innerText = "1";
				document.getElementsByClassName("cart-icon")[0].append(piece);

				setCartStatus(true);
			}

			let sum = document.getElementById("sum");
			if (sum != null){
				sum.innerText = (parseFloat(sum.innerText) + dish.price*dish.discount).toFixed(2);
			}else {
				sum = document.createElement("span");
				sum.setAttribute("class", "money-count");
				sum.setAttribute("id", "sum");
				sum.innerText = (dish.price*dish.discount*value.getAttribute("value")).toFixed(2);
				let icon = document.createElement("span");
				icon.setAttribute("class", "money-icon");
				icon.innerText = "￥";
				document.getElementsByClassName("cart-info")[0].append(icon);
				document.getElementsByClassName("cart-info")[0].append(sum);
			}
		}
	};
	xmlhttp.open("POST","/ps_project/diner/dish/"+mid,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("id="+dishID+"&act=add");
}

function dish_min(dishID, mid) {
	let xmlhttp;
	let single_order;
	if (document.getElementById(dishID) == null){
		return;
	}else {
		single_order = document.getElementById(dishID);
	}
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState === 4 && xmlhttp.status === 200){
			let dish = JSON.parse(xmlhttp.responseText)[0];
			let value = single_order.getElementsByClassName("input-num")[0];
			let value2 = document.getElementById(`dish${dishID}`).getElementsByClassName("food-number")[0];
			if (value.getAttribute("value") !== "1"){
				value.setAttribute("value", parseInt(value.getAttribute("value")) - 1 + "");
				value2.setAttribute("value", parseInt(value2.getAttribute("value")) - 1 + "");
				single_order.getElementsByClassName("replace-money")[0].innerHTML = "￥" + (dish.price*dish.discount*value.getAttribute("value")).toFixed(2);
			}else {
				single_order.remove();
			}

			let piece = document.getElementById("piece");
			if (piece != null){
				if (parseInt(piece.innerText) !== 1){
					piece.innerText = parseInt(piece.innerText) - 1 +"";
				}else {
					piece.remove();
					setCartStatus(false);
					document.getElementById(`dish${dishID}`).getElementsByClassName("food-introduce-addcart")[0].removeAttribute("style");
					document.getElementById(`dish${dishID}`).getElementsByClassName("food-introduce-form")[0].removeAttribute("style");
				}
			}

			let sum = document.getElementById("sum");
			if (sum != null){
				sum.innerText = (parseFloat(sum.innerText) - dish.price*dish.discount).toFixed(2);
				if (parseFloat(sum.innerText) === 0.0){
					document.getElementsByClassName("cart-info")[0].lastChild.remove();
					document.getElementsByClassName("cart-info")[0].lastChild.remove();
				}
			}
		}
	};
	xmlhttp.open("POST","/ps_project/diner/dish/"+mid,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("id="+dishID+"&act=min");
}

function loadCartFromSession(order_info, mid) {
	let cart_basket = document.getElementById("cart_basket");
	let piece = document.createElement("span");
	piece.setAttribute("class", "cart-pieces");
	piece.setAttribute("id", "piece");
	let piece_num=0;

	let sum = document.createElement("span");
	sum.setAttribute("class", "money-count");
	sum.setAttribute("id", "sum");
	let sum_num = 0;

	for (let dish in order_info){
		let single_order = createSingleOrder(dish, mid);
		cart_basket.insertBefore(single_order,cart_basket.firstChild);
		single_order.getElementsByClassName("order-food-name")[0].children[0].innerText = order_info[dish].name;
		single_order.getElementsByTagName("input")[0].setAttribute("value", dish);
		single_order.getElementsByClassName("input-num")[0].setAttribute("value", order_info[dish].number);
		single_order.getElementsByClassName("replace-money")[0].innerHTML = "￥" + (order_info[dish].price * order_info[dish].number).toFixed(2);
		piece_num += order_info[dish].number;
		sum_num += order_info[dish].number*order_info[dish].price;

		let value2 = document.getElementById(`dish${dish}`).getElementsByClassName("food-number")[0];
		value2.setAttribute("value", order_info[dish].number);
		document.getElementById(`dish${dish}`).getElementsByClassName("food-introduce-addcart")[0].setAttribute("style", "display: none");
		document.getElementById(`dish${dish}`).getElementsByClassName("food-introduce-form")[0].setAttribute("style", "display: table");

	}
	if (piece_num !== 0){
		piece.innerText = piece_num + "";
		document.getElementsByClassName("cart-icon")[0].append(piece);
		setCartStatus(true);
	}
	if (sum_num !== 0.0){
		sum.innerText = sum_num.toFixed(2);
		let icon = document.createElement("span");
		icon.setAttribute("class", "money-icon");
		icon.innerText = "￥";
		document.getElementsByClassName("cart-info")[0].append(icon);
		document.getElementsByClassName("cart-info")[0].append(sum);
	}
}

function createSingleOrder(dishID, mid) {
	let single_order = document.createElement('div');
	single_order.setAttribute("class", "single-order");
	single_order.setAttribute("id", dishID);
	single_order.append(document.createElement("div"));
	single_order.children[0].setAttribute("class", "order-info");

	let food_name = document.createElement('div');
	food_name.setAttribute("class", "order-food-name");
	food_name.append(document.createElement("p"));

	let num_form = document.createElement('div');
	num_form.setAttribute("class", "select-num-form");
	num_form.append(document.createElement("div"));
	num_form.children[0].setAttribute("class", "order-num-select");

	num_form.children[0].append(document.createElement("button"));
	num_form.children[0].children[0].setAttribute("class", "minus-num");
	num_form.children[0].children[0].setAttribute("type", "button");
	num_form.children[0].children[0].setAttribute("onclick", `dish_min(${dishID}, ${mid})`);
	num_form.children[0].children[0].innerHTML = "-";

	num_form.children[0].append(document.createElement("input"));
	num_form.children[0].children[1].setAttribute("class", "input-num");
	num_form.children[0].children[1].setAttribute("id", "number");
	num_form.children[0].children[1].setAttribute("oninput", `dish_change(${dishID}, ${mid}, this.value)`);
	num_form.children[0].children[1].setAttribute("value", "");

	num_form.children[0].append(document.createElement("button"));
	num_form.children[0].children[2].setAttribute("class", "add-num");
	num_form.children[0].children[2].setAttribute("type", "button");
	num_form.children[0].children[2].setAttribute("onclick", `dish_add(${dishID}, ${mid})`);
	num_form.children[0].children[2].innerHTML = "+";

	let money = document.createElement("div");
	money.setAttribute("class", "money-of-this-food");
	money.append(document.createElement("span"));
	money.children[0].setAttribute("class", "replace-money");

	single_order.children[0].append(food_name);
	single_order.children[0].append(num_form);
	single_order.children[0].append(money);

	return single_order;
}

function cleanCart(mid) {
	let xmlhttp;
	if (window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState === 4 && xmlhttp.status === 200){
			document.getElementById("cart_basket").innerHTML = "";
			let button_add = document.getElementsByClassName("food-introduce-addcart");
			for (let i=0; i < button_add.length; i++){
				button_add[i].removeAttribute("style");
			}

			let input = document.getElementsByClassName("food-introduce-form");
			for (let i=0; i < button_add.length; i++){
				input[i].removeAttribute("style");
			}


			document.getElementById("piece").remove();
			if (document.getElementById("sum") != null){
				document.getElementsByClassName("cart-info")[0].lastChild.remove();
				document.getElementsByClassName("cart-info")[0].lastChild.remove();
			}
			setCartStatus(false);
		}
	};
	xmlhttp.open("POST", "/ps_project/diner/dish/"+mid,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("act=clean");
}

function setCartStatus(status) {
	let cart_status = document.getElementsByClassName("cart-status")[0];
	if (status){
		cart_status.setAttribute("style", "background-color: #51d862");
		cart_status.children[0].innerText = "去结算 >";
		cart_status.children[0].setAttribute("style", "color: #fff")
	}else {
		cart_status.setAttribute("style", "background-color: #e4e4e4");
		cart_status.children[0].innerText = "购物车是空的";
		cart_status.children[0].setAttribute("style", "color: #333")
	}

}

function dish_change(dishID, mid, number) {
	let xmlhttp;
	let single_order = document.getElementById(dishID);
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}
	let number_int = parseInt(number);
	if (!(number_int >= 1)){
		number_int = 1;
	}
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState === 4 && xmlhttp.status === 200){
			let dish = JSON.parse(xmlhttp.responseText);

			single_order.getElementsByClassName("replace-money")[0].innerHTML = "￥" + (dish.price * number_int).toFixed(2);

			let piece = document.getElementById("piece");
			piece.innerText = (parseInt(piece.innerText) + number_int - parseInt(dish.number)) + "";

			let sum = document.getElementById("sum");
			sum.innerText = (parseFloat(sum.innerText) + dish.price*(number_int - dish.number)).toFixed(2);

			document.getElementById(`dish${dishID}`).getElementsByClassName("food-number")[0].setAttribute("value", number_int+"");
			document.getElementById(dishID).getElementsByClassName("input-num")[0].setAttribute("value", number_int+"");
		}
	};
	xmlhttp.open("POST","/ps_project/diner/dish/"+mid,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("id="+dishID+"&act=set&number="+number_int);
}
